from  django import forms
from .models import *
class RegisterForm(forms.Form):
    account = forms.EmailField(max_length=20,error_messages={'max_length':'长度不能超过20',
                                                             'required':'用户名不能为空'})
    password = forms.CharField(min_length=32,max_length=32,
                               error_messages={'max_length': '长度不能超过32',
                               'min_length':'长度不能小于6' ,'required': '密码不能为空'})
    def clean(self):
        forms.Form.clean(self)
        email = self.cleaned_data.get('account','')
        if email:
            import re
            if not re.match(r'[a-zA-Z0-9_-]{6,}@[a-zA-Z0-9_-]+(\.[A-Za-z0-9_-]+)+$',email):
                self.errors['account']=['邮箱格式不正确']
            elif User.is_exist(email):
                self.errors['account']=['用户已存在']